<?php

namespace addons\helpcenter\controller;

use addons\helpcenter\Helpcenter as baseHelpcenter;
use addons\helpcenter\server\Helpcenter as helpServer;

/**
 * Class Customform
 * @package addons\customform\controller
 */
class Helpcenter extends baseHelpcenter {

    public function __construct() {
        parent::__construct();
    }

    public function questionList() {
        $page_index = request()->post("page_index", 1);
        $page_size = request()->post("page_size", PAGESIZE);
        $search_text = request()->post("search_text", '');
        $helpServer = new helpServer();
        $condition = array(
            'vq.website_id' => $this->website_id,
            'vq.title' => array(
                'like',
                '%' . $search_text . '%'
            )
        );
        $list = $helpServer->questionList($page_index, $page_size, $condition);
        return $list;
    }
    /**
     * 添加问题
     * @return \multitype|void
     */
    public function addQuestion() {
        $data['title'] = request()->post('title', '');
        $data['cate_id'] = request()->post('cate_id', 0);
        $data['content'] = $_POST['content'];//request()->post('content', '');
        $data['sort'] = request()->post('sort', 0);
        $data['status'] = request()->post('status', 0);
        $article = new helpServer();
        $result = $article->addQuestion($data);
        if ($result > 0) {
            $this->addUserLog('添加问题', $result);
        }
        return AjaxReturn($result);
    }

    /**
     * 修改问题
     */
    public function updateQuestion() {
        $data['title'] = request()->post('title', '');
        $data['question_id'] = request()->post('question_id', '');
        $data['cate_id'] = request()->post('cate_id', 0);
        $data['content'] = $_POST['content'];//request()->post('content', '');
        $data['sort'] = request()->post('sort', 0);
        $data['status'] = request()->post('status', 0);
        $article = new helpServer();
        $result = $article->updateQuestion($data);
        if ($result > 0) {
            $this->addUserLog('修改问题', $result);
        }
        return AjaxReturn($result);
    }
    
    /*
     * 删除问题
     */
    public function deleteQuestion(){
        $questionId = request()->post("question_id",0);
        if(!$questionId){
            return AjaxReturn(0);
        }
        $helpServer = new helpServer();
        $retval = $helpServer->deleteQuestion($questionId);
        if($retval <= 0){
            return AjaxReturn($retval);
        }
        $this->addUserLog('删除问题', $retval);
        return AjaxReturn(1);
    }
    /*
     * 修改问题是否显示
     */
    public function changeQuestionShow()
    {
        $helpServer = new helpServer();
        $question_id  = request()->post('question_id',0);
        $status  = request()->post('status','');
        $res = $helpServer->updateQuestionShow($question_id, $status);
        if($res > 0){
            $this->addUserLog('修改问题是否显示', $res);
        }
        return AjaxReturn($res);
    }
    
    /*
     * 修改问题是否显示
     */
    public function changeQuestionSort()
    {
        $helpServer = new helpServer();
        $question_id  = request()->post('question_id',0);
        $sort  = request()->post('sort','');
        $res = $helpServer->updateQuestionSort($question_id, $sort);
        if($res > 0){
            $this->addUserLog('修改问题排序', $res);
        }
        return AjaxReturn($res);
    }
    
    /**
     * 添加分类
     * @return \multitype|void
     */
    public function addCate() {
        $data['name'] = request()->post('name', '');
        $data['sort'] = request()->post('sort', 0);
        $helpServer = new helpServer();
        $result = $helpServer->addCate($data);
        if ($result > 0) {
            $this->addUserLog('添加分类', $result);
        }
        return AjaxReturn($result);
    }

    /*
     * 修改分类名称
     */
    public function changeCateName()
    {
        $helpServer = new helpServer();
        $cate_id  = request()->post('cate_id',0);
        $name  = request()->post('name','');
        $res = $helpServer->updateCateName($cate_id, $name);
        if($res > 0){
            $this->addUserLog('修改分类名称', $res);
        }
        return AjaxReturn($res);
    }
    /*
     * 修改分类是否显示
     */
    public function changeCateShow()
    {
        $helpServer = new helpServer();
        $cate_id  = request()->post('cate_id',0);
        $status  = request()->post('status','');
        $res = $helpServer->updateCateShow($cate_id, $status);
        if($res > 0){
            $this->addUserLog('修改分类是否显示', $res);
        }
        return AjaxReturn($res);
    }
    /*
     * 修改分类排序
     */
    public function changeCateSort()
    {
        $helpServer = new helpServer();
        $cate_id  = request()->post('cate_id',0);
        $sort  = request()->post('sort','');
        $res = $helpServer->updateCateSort($cate_id, $sort);
        if($res > 0){
            $this->addUserLog('修改分类排序', $res);
        }
        return AjaxReturn($res);
    }
    /*
     * 问题分类列表
     */
    public function questionCateList() {
        $order = request()->post("order", 0);
        $search_text = request()->post("search_text", '');
        $helpServer = new helpServer();
        $condition = array(
            'website_id' => $this->website_id,
            'name' => array(
                'like',
                '%' . $search_text . '%'
            )
        );
        $orderBy = 'sort asc';
        if($order){
            $orderBy = 'create_time desc';
        }
        $list = $helpServer->questionCateList(1, 0, $condition, $orderBy);
        return $list;
    }
    
    /*
     * 删除分类
     */
    public function deleteCate(){
        $cateId = request()->post("cate_id",0);
        if(!$cateId){
            return AjaxReturn(0);
        }
        $helpServer = new helpServer();
        $retval = $helpServer->deleteCate($cateId);
        if($retval <= 0){
            return AjaxReturn($retval);
        }
        $this->addUserLog('删除分类', $retval);
        return AjaxReturn(1);
    }

}
